package cn.edu.abtu.service.impl;

import cn.edu.abtu.dao.CrmBannerDao;
import cn.edu.abtu.entity.CrmBanner;
import cn.edu.abtu.service.CrmBannerService;
import cn.edu.abtu.vo.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CrmBannerServiceImpl implements CrmBannerService {

    @Autowired
    private CrmBannerDao crmBannerDao;

    @Override
    public ResponseData addBanner(CrmBanner crmBanner) {
        int insert = crmBannerDao.insert(crmBanner);
        if (insert>0){
            return ResponseData.success(crmBanner.getTitle()+"上传成功",null);
        }else {
            return ResponseData.fail(crmBanner.getTitle()+"上传失败",null);
        }
    }

    @Override
    public ResponseData deleteBanner(String bannerId) {
        CrmBanner crmBanner = crmBannerDao.selectById(bannerId);
        int deleteById = crmBannerDao.deleteById(bannerId);
        if (deleteById>0){
            return ResponseData.success(crmBanner.getTitle()+"删除成功",null);
        }else {
            return ResponseData.fail(crmBanner.getTitle()+"删除失败",null);
        }
    }

    @Override
    @Cacheable(key = "'selectIndexBanner'" ,value = "banner")
    public ResponseData listBanner() {

        QueryWrapper<CrmBanner> bannerQueryWrapper = new QueryWrapper<>();
        bannerQueryWrapper.orderByDesc("id");
        bannerQueryWrapper.last("limit 4"); //在Sql语句后拼接一个limit 4
        List<CrmBanner> crmBanners = crmBannerDao.selectList(bannerQueryWrapper);
        if (crmBanners.size()>0){
            return ResponseData.success("查询成功",crmBanners);
        }else {
            return ResponseData.fail("查询失败",null);
        }
    }
}
